/**
 *	@file	np_sub.c
 *	@brief	木の実プランターアプリ サブ
 *	@author	Miyuki Iwasawa
 *	@date	08.08.04
 *
 */

#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"
#include "itemtool/item.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_n_planter.h"

#include "savedata/config.h"
#include "application/nut_planter.h"
#include "np_sys.h"
#include "np_tool.h"
#include "np_app.h"
#include "np_sub.h"
#include "np_time.h"
#include "np_key.h"
#include "np_snd_def.h"
#include "nuts.h"

#include "nplanter_gra.naix"
#include "data/clact_util/clact_header.naix"

//選択ボックスリストデータ
typedef struct _NP_SBOX_LIST{
	int num;
	const u8* list;
}NP_SBOX_LIST;

//水遣りポット移動エフェクト制御構造体
typedef struct _POT_MOVE_EFF{
	NP_APPWORK*	app_wk;

	u8	seq;
	u8	ct;
	u8	now_pos;
	u8	next_pos;

	u8	time;
	s8	dir;
	u8	old_pos;	//1フレーム前のポット位置
}POT_MOVE_EFF;

//収穫木の実アイコンエフェクト制御構造体
typedef struct _HARVEST_ICON_EFF{
	NP_APPWORK*	app_wk;

	u8	seq;
	u8	ct;
	u8	id;

	CATS_ACT_PTR		act;
}HARVEST_ICON_EFF;


////////////////////////////////////////////////////////////////
///データ定義
static const u8 nplanter_mlist01[] = {
 msg_com_compost, msg_com_plant, msg_com_cancel
};
static const u8 nplanter_mlist02[] = {
 msg_com_plant, msg_com_cancel
};
static const u8 nplanter_mlist03[] = {
 msg_com_water, msg_com_cancel
};
static const u8 nplanter_mlist04[] = {
 msg_com_harvest, msg_com_cancel
};

static const NP_SBOX_LIST nplanter_sbox_dat[NP_SBOX_MAX] = {
 { 3, nplanter_mlist01 },
 { 2, nplanter_mlist02 },
 { 2, nplanter_mlist03 },
 { 2, nplanter_mlist04 },
};

///セレクトボックス　ヘッダデータ構造体
static const SELBOX_HEAD_PRM data_sbox_sel = {
	TRUE,SBOX_OFSTYPE_CENTER,	///<ループフラグ、左寄せorセンタリング

	4,	///<項目表示オフセットX座標(dot)

	DFRM_MSG,				///<フレームNo
	SBOX_PAL,				///<フレームパレットID
	0,0,					///oam BG Pri,Software Pri
	SBOX_FCGX,SBOX_WCGX,	///<ウィンドウcgx,フレームcgx
	SBOX_FCGX_SIZ,		///<BMPウィンドウ占有キャラクタ領域サイズ(char)
};

static const TCATS_OAM_INIT nplanter_coi = {
	0,128,		///< メイン	OAM管理領域・開始/終了
	0,32,		///< メイン	アフィン管理領域・開始/終了
	0,128,		///< サブ	OAM管理領域・開始/終了
	0,32,		///< サブ	アフィン管理領域・開始/終了
};
static const TCATS_CHAR_MANAGER_MAKE nplanter_ccmm = {
	NPLANTER_ACTRES_MAX,	//ID制御数
	64*1024,	//メイン画面管理サイズ(byte単位) 0/64kbyte
	16*1024,	//サブ画面管理サイズ(byte単位) 0/16kbyte
	GX_OBJVRAMMODE_CHAR_1D_32K,	//メインOBJモード指定
	GX_OBJVRAMMODE_CHAR_1D_32K,	//サブOBJモード指定
};

///TCATS_RES_ARC
static const TCATS_RESOURCE_FILE_LIST nplanter_actres_list = {
	NARC_clact_header_nplanter_chr_resdat,	// キャラクタ
	NARC_clact_header_nplanter_pal_resdat,		// パレット
	NARC_clact_header_nplanter_cell_resdat,	// セル
	NARC_clact_header_nplanter_canm_resdat,	// セルアニメ
	TCATS_RESID_NULL,							// マルチセル
	TCATS_RESID_NULL,							// マルチセルアニメ
	NARC_clact_header_nplanter_celact_cldat		// ヘッダー
};


////////////////////////////////////////////////////////////////
///プロトタイプ
////////////////////////////////////////////////////////////////
static void NPlanterVBlank(void * work);

static void sub_VBankSet(void);
static void sub_BGLInit(NP_APPWORK* wk);
static void sub_BGLRelease(NP_APPWORK* wk);
static void sub_2DResInit(NP_APPWORK* wk);
static void sub_2DResRelease(NP_APPWORK* wk);
static void sub_MsgDataInit(NP_APPWORK* wk);
static void sub_MsgDataRelease(NP_APPWORK* wk);
static void sub_BmpWinAdd(NP_APPWORK* wk);
static void sub_BmpWinDel(NP_APPWORK* wk);
static void sub_SelectListInit(NP_APPWORK* wk);
static void sub_SelectListRelease(NP_APPWORK* wk);

static void sub_ActorSysInit(NP_APPWORK* wk);
static void sub_ActorSysRelease(NP_APPWORK* wk);
static void sub_ActorInit(NP_APPWORK* wk);
static void sub_ActorRelease(NP_APPWORK* wk);
static void sub_ActorObjMake(NP_APPWORK* wk);
static void sub_ActorObjDelete(NP_APPWORK* wk);

static void sub_SeedActorResChange(NP_APPWORK* wk, BED_ID id);
static void sub_IconActorResChange(NP_APPWORK* wk, BED_ID id);
static CATS_ACT_PTR sub_IconActorAdd(NP_APPWORK* wk,s16 x,s16 y,u8 spri);

static void sub_PotMoveEffSet(NP_APPWORK* wk);
static void tcb_PotMoveEff( TCB_PTR tcb, void* p_work );
static void sub_HarvestIconEffSet(NP_APPWORK* wk);
static void tcb_HarvestIconEff( TCB_PTR tcb, void* p_work );

static void sub_MsgMake(NP_APPWORK* wk,MSG_IDX idx,BED_ID bed_id);

#ifdef PM_DEBUG
static void deb_BmpWinAdd(NP_APPWORK* wk);
static void deb_BmpWinDel(NP_APPWORK* wk);
#endif	//PM_DEBUG


////////////////////////////////////////////////////////////////
///グローバル関数
////////////////////////////////////////////////////////////////
/**
 *	@brief	リソース初期化
 */
void NPlanterResInit(NP_APPWORK* wk)
{
	sub_BGLInit( wk );
	sub_2DResInit( wk );
	sub_MsgDataInit( wk );
	sub_BmpWinAdd( wk );
	sub_SelectListInit( wk );
	sub_ActorInit( wk );

#ifdef PM_DEBUG
	deb_BmpWinAdd( wk );
#endif	//PM_DEBUG
	sys_VBlankFuncChange(NPlanterVBlank, wk);
}

/**
 *	@brief	リソース解放
 */
void NPlanterResRelease(NP_APPWORK* wk)
{
#ifdef PM_DEBUG
	deb_BmpWinDel( wk );
#endif	//PM_DEBUG

	sub_ActorRelease( wk );
	sub_SelectListRelease( wk );
	sub_BmpWinDel( wk );
	sub_MsgDataRelease( wk );
	sub_2DResRelease( wk );
	sub_BGLRelease( wk );
}

/**
 *	@brief	初期画面描画
 */
void NPlanterInitialDraw(NP_APPWORK* wk)
{
	int i;

	NPSub_BackButtonWrite(wk,BACK_BTN_ACTIVE);
	NPSub_CursorPosSet( wk );

	switch(wk->ret_mode){
	case 1:
		NPSub_MsgPut(wk,MIDX_RA_COMPOST,wk->c_pos,TRUE);
		NPSub_ActAnmControl(wk,FALSE);
		break;
	case 2:
		NPSub_MsgPut(wk,MIDX_RA_PLANT,wk->c_pos,TRUE);
		NPSub_ActAnmControl(wk,FALSE);
		break;
	case 0:
	default:
		NPSub_MsgPut(wk,MIDX_BED_SELECT,BED_NULL,TRUE);
		NPSub_ActAnmControl(wk,TRUE);
	}
	NPTime_Update( wk, FALSE );

	//木の実の初期描画状態セット
	for(i = 0;i < SEEDBED_MAX;i++){
		sub_SeedActorResChange( wk, i);
		NPSub_SeedActorSet( wk, i );
	}
#ifdef PM_DEBUG
	Deb_SeedPrint( wk );
#endif	//PM_DEBUG
}

/**
 *	@brief	サブシーケンス　水遣りエフェクト
 */
BOOL NPSubSeq_WaterEffect(NP_APPWORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		wk->pot_pos = wk->c_pos;
		wk->pot_npos = 0xFF;

		CATS_ObjectPosSet(wk->pAct[ACT_POT],wk->pot_pos*ACT_POT_OX+ACT_POT_PX,ACT_POT_PY);
		CLACT_AnmChg(wk->pAct[ACT_POT],ANM_POT01);
		CLACT_AnmReStart(wk->pAct[ACT_POT]);
		CLACT_SetAnmFlag(wk->pAct[ACT_POT], TRUE);
		CLACT_SetDrawFlag(wk->pAct[ACT_POT], TRUE );
		wk->wait = 0;
		wk->sub_seq++;
		break;
	case 1:
		if(wk->wait++ < 15){
			break;
		}
		Snd_SePlay(SND_NP_WATER);
		CLACT_AnmReStart(wk->pAct[ACT_POT]);
		CLACT_AnmChg(wk->pAct[ACT_POT],ANM_POT02);
		CLACT_SetAnmFlag(wk->pAct[ACT_CURSOR], TRUE );
		wk->wait = 0;
		wk->sub_seq++;
		break;
	case 2:
		//水遣りエフェクト
		if(wk->wait++ == 0){
			NPSub_Water( wk ,wk->pot_pos);
		}
		if(wk->wait < 30){
			//次の予約を受け付ける
			NPKey_InputWaterSelect(wk);
			break;
		}
		wk->wait = 0;
		if(wk->pot_npos == 0xFF){	//終了
			Snd_SeStopBySeqNo(SND_NP_WATER,2);
			CLACT_AnmChg(wk->pAct[ACT_POT],ANM_POT01);
			wk->sub_seq = 4;
			break;
		}
		//ポット移動エフェクトセット
		sub_PotMoveEffSet(wk);
		wk->sub_seq++;
		break;
	case 3:	//ポット移動待ち
		if(wk->eff_tcb_ct){
			break;
		}
		wk->wait = 0;
		wk->pot_pos = wk->pot_npos;
		wk->pot_npos = 0xFF;
		CLACT_SetAnmFlag(wk->pAct[ACT_CURSOR], TRUE);
		wk->sub_seq = 2;
		break;
	case 4:	//終了
		if(wk->wait++ < 15){
			break;
		}

		//状態ワークアップデート
		NPTime_SeedStateUpdate( wk );
		#ifdef PM_DEBUG
		Deb_SeedPrint( wk );
		#endif	//PM_DEBUG

		CLACT_SetDrawFlag(wk->pAct[ACT_POT], FALSE );
		CLACT_SetAnmFlag(wk->pAct[ACT_CURSOR], FALSE );
		wk->wait = 0;
		wk->sub_seq = 0;
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	サブシーケンス　収穫エフェクト
 */
BOOL NPSubSeq_HarvestEffect(NP_APPWORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		sub_IconActorResChange( wk, wk->c_pos );
		wk->harvest_num = wk->seed[wk->c_pos].nuts_ct;
		wk->wait = 0;
		wk->sub_seq++;
		break;
	case 1:
		//木の実エフェクト登録
		if(wk->wait == 0){
			sub_HarvestIconEffSet( wk );
			--wk->harvest_num;
			wk->wait = 8;
		}
		--wk->wait;
		if(wk->harvest_num > 0){
			break;
		}
		wk->sub_seq++;
		break;
	case 2:
		//木の実エフェクト終了待ち
		if(wk->eff_tcb_ct){
			break;
		}
		wk->wait = 0;
		wk->sub_seq = 0;
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	戻るボタン描画
 */
void NPSub_BackButtonWrite(NP_APPWORK* wk,BACK_BTN_STATE state)
{
	u32 siz;
	static const GF_PRINTCOLOR col[] = {
	 GF_PRINTCOLOR_MAKE(1,2,0),
	 GF_PRINTCOLOR_MAKE(3,4,0),
	 GF_PRINTCOLOR_MAKE(5,6,0),
	};

	CLACT_AnmChg(wk->pAct[ACT_BACK], ANM_BACK+state);
	CLACT_AnmReStart( wk->pAct[ACT_BACK]);

	GF_BGL_BmpWinDataFill( &wk->win[WIN_BACK], 0);

	siz = FontProc_GetPrintStrWidth( FONT_TOUCH, wk->msg.back, 0 );
	GF_STR_PrintColor( &wk->win[WIN_BACK], FONT_TOUCH, wk->msg.back,
		((BMP_BACK_SX*8)-siz)/2,0, MSG_NO_PUT, col[0], NULL );

	GF_BGL_BmpWinOnVReq(&wk->win[WIN_BACK]);
}

/**
 *	@brief	カーソルポジション描画
 */
void NPSub_CursorPosSet(NP_APPWORK* wk)
{
	u8 pos = wk->c_pos;

	CATS_ObjectPosSet(wk->pAct[ACT_CURSOR],pos*ACT_CUR_OX+ACT_CUR_PX,ACT_CUR_PY);

	if(wk->seed[pos].stat > SEEDSTAT_MIKI){
		CLACT_AnmChg(wk->pAct[ACT_CURSOR], ANM_CUR02 );
	}else{
		CLACT_AnmChg(wk->pAct[ACT_CURSOR], ANM_CUR01 );
	}
}

/**
 *	@brief	カーソル描画状態切替
 */
void NPSub_CursorVisibleSet(NP_APPWORK* wk,BOOL draw_f)
{
	CLACT_SetDrawFlag(wk->pAct[ACT_CURSOR], draw_f );
}

/*
 *	@brief	カーソル＆木の実　アニメ状態制御
 */
void NPSub_ActAnmControl(NP_APPWORK* wk,BOOL anm_f)
{
	int i;

	CLACT_SetAnmFlag(wk->pAct[ACT_CURSOR], anm_f);

	for(i = 0;i < SEEDBED_MAX;i++){
		if(wk->seed[i].act != NULL && wk->seed[i].stat >= SEEDSTAT_HUTABA){
			CLACT_SetAnmFlag(wk->seed[i].act, anm_f);
		}
	}
}

/**
 *	@brief	ウィンドウメッセージ描画
 */
void NPSub_MsgPut(NP_APPWORK* wk,MSG_IDX idx,BED_ID bed_id,BOOL frame)
{
	GF_BGL_BMPWIN* win;

	if(idx == MIDX_BED_SELECT){
		win = &wk->win[WIN_MSGS];
	}else{
		win = &wk->win[WIN_MSGL];
	}
	GF_BGL_BmpWinDataFill( win, 15 );
	if(frame){
		GF_BGL_ScrFill( wk->bgl ,DFRM_MSG, 0x0000, 0, 18, 32, 6, 0);
		BmpTalkWinWrite( win, WINDOW_TRANS_OFF, BMP_TALK_WIN_CGX, BMP_TALK_WIN_PAL );
	}
	//描画メッセージ展開
	sub_MsgMake(wk,idx,bed_id);

	GF_STR_PrintSimple(
			win, FONT_TALK, wk->msg.tmp,
			0, 0, MSG_NO_PUT, NULL );

	GF_BGL_BmpWinOnVReq(win);

	wk->msg_wait = 60;
}

/**
 *	@brief	選択ボックススタート
 */
void NPSub_SBoxSet(NP_APPWORK* wk,NP_SBOX_ID id)
{
	SELBOX_HEADER head;

	MI_CpuClear8(&head,sizeof(SELBOX_HEADER));

	head.prm = data_sbox_sel;
	head.list = (const BMPLIST_DATA*)wk->slist[id];
	head.bgl = wk->bgl;

	head.count = nplanter_sbox_dat[id].num;

	wk->list_wk = SelectBoxSet(wk->sbox,&(head),wk->key_mode,
			SBOX_PX,SBOX_PY-(head.count*3),SBOX_W,0);
}

/**
 *	@brief	選択ボックス待ち
 */
u32 NPSub_SBoxWait(NP_APPWORK* wk)
{
	u32 ret;

	ret = SelectBoxMain(wk->list_wk);
	if(ret == SBOX_SELECT_NULL){
		return SBOX_SELECT_NULL;
	}
	wk->key_mode = SelectBoxGetKTStatus(wk->list_wk);
	SelectBoxExit(wk->list_wk);

	return ret;
}

/**
 *	@brief	木の実アクターオブジェクト生成
 */
void NPSub_SeedActorSet(NP_APPWORK* wk,BED_ID id )
{
	TCATS_OBJECT_ADD_PARAM prm;
	SEED_STATE* st = &wk->seed[id];

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam =
	{	// 土
		ACTRES_NORMAL,	//ID
		ACT_SEED_PX, ACT_SEED_PY, 0,	//x,y,z
		ANM_BED01, ACT_SPRI_SEED, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
		TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
	};

	if(st->o_stat != SEEDSTAT_NOTHING && st->act != NULL){
		CLACT_Delete(st->act);	//前の状態を削除
	}
	if(st->stat == SEEDSTAT_NOTHING){
		st->act = NULL;
		CLACT_AnmFrameSet( wk->pAct[ACT_SOIL01+id] ,SEEDGROUND_FULL);
		return;
	}
	//現在の状態を登録
	MI_CpuCopy8(&ActAddParam,&prm,sizeof(TCATS_OBJECT_ADD_PARAM));

	prm.x = ACT_SEED_PX+ACT_SEED_OX*id;

	switch(st->stat){
	case SEEDSTAT_UNDERGROUND:
		prm.anm = ANM_BED01;
		break;
	case SEEDSTAT_HUTABA:
		prm.anm = ANM_BUD;
		break;
	default:
		prm.no = ACTRES_SEED01+id;
		prm.anm = SANM_TRUNK+(st->stat-SEEDSTAT_MIKI);
		break;
	}
	st->act = CATS_ObjectAdd( wk->pActSys, wk->pActRes, &prm );

//	CLACT_AnmFrameSet( wk->pAct[idx] , wk->seed[i].ground_st);
	CLACT_SetDrawFlag(st->act, TRUE );
	if(st->stat == SEEDSTAT_UNDERGROUND){
		CLACT_SetAnmFlag(st->act, FALSE);
		CLACT_AnmFrameSet( st->act , wk->seed[id].ground_st);
	}else{
		CLACT_SetAnmFlag(st->act, TRUE);
	}
	CLACT_AnmFrameSet( wk->pAct[ACT_SOIL01+id] ,wk->seed[id].ground_st);
}

/*
 *	@brief	水遣り処理
 */
void NPSub_Water(NP_APPWORK* wk,BED_ID id)
{
	SEED_STATE* st = &wk->seed[id];

	//水遣り
	SEEDBED_SetSeedWater( wk->seed_bed, id );
	NPTime_SeedStateUpdate( wk );
	//土を元に戻す
	CLACT_AnmFrameSet( wk->pAct[ACT_SOIL01+id] ,SEEDGROUND_FULL);
	if(st->stat == SEEDSTAT_UNDERGROUND){
		CLACT_AnmFrameSet( st->act , st->ground_st);
	}
}

/**
 *	@brief	収穫処理
 */
void NPSub_Harvest(NP_APPWORK* wk)
{
	SEED_STATE* st = &wk->seed[wk->c_pos];

	//収穫
	SEEDBED_GetHarvest( wk->seed_bed, wk->c_pos );

	//手持ちアイテム追加
	MyItem_AddItem( wk->item_sv ,
		SeedSys_Nuts2Item(st->type),st->nuts_ct, wk->heapID );

	//状態ワークアップデート
	NPTime_SeedStateUpdate( wk );

	//アクターを消す
	CLACT_Delete(st->act);
	st->act = NULL;
	//土を元に戻す
	CLACT_AnmFrameSet( wk->pAct[ACT_SOIL01+wk->c_pos] ,SEEDGROUND_FULL);

	//スコア加算処理
	RECORD_Score_Add(wk->rec_sv,SCORE_ID_GET_NUTS);
}

////////////////////////////////////////////////////////////////
///ローカル関数
////////////////////////////////////////////////////////////////
/**
 *	@brief	VBlank関数
 */
static void NPlanterVBlank(void * work)
{
	NP_APPWORK* wk = (NP_APPWORK*)work;

	//アクター転送
	if(wk->pActSys != NULL){
		CATS_RenderOamTrans();
	}
	NNS_GfdDoVramTransfer();	//VRam転送マネージャ実行
	GF_BGL_VBlankFunc( wk->bgl );
	OS_SetIrqCheckFlag( OS_IE_V_BLANK);
}

/**
 *	@brief	vramバンク設定
 */
static void sub_VBankSet(void)
{
	GF_BGL_DISPVRAM vramSetTable = {
		GX_VRAM_BG_128_A,				// メイン2DエンジンのBG
		GX_VRAM_BGEXTPLTT_NONE,			// メイン2DエンジンのBG拡張パレット
		GX_VRAM_SUB_BG_128_C,			// サブ2DエンジンのBG
		GX_VRAM_SUB_BGEXTPLTT_NONE,		// サブ2DエンジンのBG拡張パレット
		GX_VRAM_OBJ_64_E,				// メイン2DエンジンのOBJ
		GX_VRAM_OBJEXTPLTT_NONE,		// メイン2DエンジンのOBJ拡張パレット
		GX_VRAM_SUB_OBJ_16_I,			// サブ2DエンジンのOBJ
		GX_VRAM_SUB_OBJEXTPLTT_NONE,	// サブ2DエンジンのOBJ拡張パレット
		GX_VRAM_TEX_NONE,				// テクスチャイメージスロット
		GX_VRAM_TEXPLTT_NONE			// テクスチャパレットスロット
	};
	GF_Disp_SetBank( &vramSetTable );
}
/*
 *	@brief	BGL初期化
 */
static void sub_BGLInit(NP_APPWORK* wk)
{
	sub_VBankSet();

	wk->bgl = GF_BGL_BglIniAlloc(wk->heapID);
	{	//BG SYSTEM
		GF_BGL_SYS_HEADER BGsys_data = {
			GX_DISPMODE_GRAPHICS,GX_BGMODE_0,GX_BGMODE_0,GX_BG0_AS_2D
		};
		GF_BGL_InitBG(&BGsys_data);
	}
	//上画面をサブ,下画面をメインに切り替え
	GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);

	{	// 上画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			UFRM_MSG_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_MSG, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_MSG );
	}

	{	// 上画面　背景
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			UFRM_BACK_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, UFRM_BACK, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, UFRM_BACK );
	}

	{	// 下画面　テキスト面
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf800, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			DFRM_MSG_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_MSG, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_MSG );
	}
	{	// 下画面　テキスト面2
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000, GX_BG_CHARBASE_0x10000, GX_BG_EXTPLTT_01,
			DFRM_MSG2_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_MSG2, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_MSG2 );
	}

	{	// 下画面　背景
		GF_BGL_BGCNT_HEADER TextBgCntDat = {
			0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
			DFRM_BACK_PRI, 0, 0, FALSE
		};
		GF_BGL_BGControlSet( wk->bgl, DFRM_BACK, &TextBgCntDat, GF_BGL_MODE_TEXT );
		GF_BGL_ScrClear( wk->bgl, DFRM_BACK );
	}


	GF_BGL_ClearCharSet( UFRM_MSG, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( UFRM_BACK, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( DFRM_MSG, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( DFRM_MSG2, 32, 0, wk->heapID );
	GF_BGL_ClearCharSet( DFRM_BACK, 64, 0, wk->heapID );
}

/**
 * @brief	BGL解放
 */
static void sub_BGLRelease(NP_APPWORK* wk)
{

	GF_BGL_BGControlExit(wk->bgl, DFRM_BACK );
	GF_BGL_BGControlExit(wk->bgl, DFRM_MSG2 );
	GF_BGL_BGControlExit(wk->bgl, DFRM_MSG );
	GF_BGL_BGControlExit(wk->bgl, UFRM_BACK );
	GF_BGL_BGControlExit(wk->bgl, UFRM_MSG );

	sys_FreeMemoryEz(wk->bgl);

	//上画面をメイン,下画面をサブに戻す
	GX_SetDispSelect(GX_DISP_SELECT_MAIN_SUB);
}
/**
 * @brief	2Dリソース初期化
 */
static void sub_2DResInit(NP_APPWORK* wk)
{
	ARCHANDLE* handle;

	//アーカイブのハンドルを取得
	handle = ArchiveDataHandleOpen(ARC_NUT_PLANTER_GRA,wk->heapID);

	//メイン下画面背景転送
	ArcUtil_HDL_PalSet( handle,
			NARC_nplanter_gra_planter_d_nclr,PALTYPE_MAIN_BG, 0, 0, wk->heapID);
	ArcUtil_HDL_BgCharSet( handle,
			NARC_nplanter_gra_planter_d_ncgr, wk->bgl, DFRM_BACK, 0, 0, FALSE, wk->heapID);
	ArcUtil_HDL_ScrnSet( handle,
			NARC_nplanter_gra_planter_d_nscr, wk->bgl, DFRM_BACK, 0, 0, FALSE, wk->heapID);

	//サブ上画面背景転送
	ArcUtil_HDL_PalSet( handle,
			NARC_nplanter_gra_planter_u_nclr,PALTYPE_SUB_BG, 0, 0, wk->heapID);
	ArcUtil_HDL_BgCharSet( handle,
			NARC_nplanter_gra_planter_u_ncgr, wk->bgl, UFRM_BACK, 0, 0, FALSE, wk->heapID);
	ArcUtil_HDL_ScrnSet( handle,
			NARC_nplanter_gra_planter_u_nscr, wk->bgl, UFRM_BACK, 0, 0, FALSE, wk->heapID);

	wk->seed_h = handle;

	//会話ウインドウグラフィック転送
	TalkWinGraphicSet(wk->bgl,DFRM_MSG,
		BMP_TALK_WIN_CGX, BMP_TALK_WIN_PAL,wk->win_type, wk->heapID);
	TalkFontPaletteLoad( PALTYPE_MAIN_BG, TALK_FONT_PAL*0x20, wk->heapID );

}

/**
 * @brief	2Dリソース解放
 */
static void sub_2DResRelease(NP_APPWORK* wk)
{
	//ハンドル解放
	ArchiveDataHandleClose( wk->seed_h );
}

/**
 *	@brief	メッセージデータ初期化
 */
static void sub_MsgDataInit(NP_APPWORK* wk)
{
	int i;

	//タッチフォントロード
	FontProc_LoadFont(FONT_TOUCH,wk->heapID);

	wk->msg.man = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,
			NARC_msg_n_planter_dat,wk->heapID);

	wk->msg.wset = WORDSET_CreateEx(WORDSET_NP_BUFNUM,WORDSET_NP_BUFLEN,wk->heapID);
	wk->msg.tmp = STRBUF_Create(NP_MSG_BUFLEN,wk->heapID);

	wk->msg.back = MSGMAN_AllocString(wk->msg.man,msg_back);
	for(i = 0;i < MIDX_EXP_MAX;i++){
		wk->msg.exp[i] = MSGMAN_AllocString(wk->msg.man,msg_exp_bed_select+i);
	}

#ifdef PM_DEBUG
	for(i = 0;i < DMIDX_MAX;i++){
		wk->msg.deb[i] = MSGMAN_AllocString(wk->msg.man,deb_print_time01+i);
	}
#endif	//PM_DEBUG
}

/**
 *	@brief	メッセージデータ解放
 */
static void sub_MsgDataRelease(NP_APPWORK* wk)
{
	int i;

#ifdef PM_DEBUG
	for(i = 0;i < DMIDX_MAX;i++){
		STRBUF_Delete(wk->msg.deb[i]);
	}
#endif	//PM_DEBUG

	//文字列バッファ削除
	for(i = 0;i < MIDX_EXP_MAX;i++){
		STRBUF_Delete(wk->msg.exp[i]);
	}
	STRBUF_Delete(wk->msg.back);
	STRBUF_Delete(wk->msg.tmp);
	WORDSET_Delete(wk->msg.wset);

	MSGMAN_Delete(wk->msg.man);

	//タッチフォントアンロード
	FontProc_UnloadFont(FONT_TOUCH);
}

/**
 *	@brief	BMPウィンドウ初期化
 */
static void sub_BmpWinAdd(NP_APPWORK* wk)
{
	int i;
	static const BMPWIN_DAT BmpWinData[] = {
	 {	// 「もどるボタン」
		BMP_BACK_FRM, BMP_BACK_PX, BMP_BACK_PY,
		BMP_BACK_SX, BMP_BACK_SY, BMP_BACK_PAL, BMP_BACK_CGX
	 },
	 {	// 「メッセージウィンドウ」
		BMP_MSG_FRM, BMP_MSG_PX, BMP_MSG_PY,
		BMP_MSG_SX01, BMP_MSG_SY, BMP_MSG_PAL, BMP_MSG_CGX01
	 },
	 {	// 「メッセージウィンドウ」
		BMP_MSG_FRM, BMP_MSG_PX, BMP_MSG_PY,
		BMP_MSG_SX02, BMP_MSG_SY, BMP_MSG_PAL, BMP_MSG_CGX02
	 },
	};

	for( i=0; i<WIN_MAX; i++ ){
		GF_BGL_BmpWinAddEx( wk->bgl, &wk->win[i], &BmpWinData[i] );
		GF_BGL_BmpWinDataFill( &wk->win[i], 0 );
	}

	//YesNoボタンシステムワーク確保
	wk->ynbtn_wk = TOUCH_SW_AllocWork(wk->heapID);
}

/**
 *	@brief	BMPウィンドウ解放
 */
static void sub_BmpWinDel(NP_APPWORK* wk)
{
	int i;
	for( i=0; i<WIN_MAX; i++ ){
		GF_BGL_BmpWinOff( &wk->win[i] );
		GF_BGL_BmpWinDel( &wk->win[i] );
	}
	//YesNoボタンシステムワーク解放
	TOUCH_SW_FreeWork( wk->ynbtn_wk );
}

/**
 *	@brief	選択ボックスリスト初期化
 */
static void sub_SelectListInit(NP_APPWORK* wk)
{
	int i,j;
	const NP_SBOX_LIST* dat;

	//BMPリスト領域作成
	for(i = 0;i < NP_SBOX_MAX;i++){
		dat = &(nplanter_sbox_dat[i]);
		wk->slist[i] = BMP_MENULIST_Create(dat->num,wk->heapID);

		for(j = 0;j < dat->num;j++){
			BMP_MENULIST_AddArchiveString(wk->slist[i],wk->msg.man,dat->list[j],j);
		}
	}
}

/**
 *	@brief	選択ボックスリスト解放
 */
static void sub_SelectListRelease(NP_APPWORK* wk)
{
	int i;

	//リスト解放
	for(i = 0;i < NP_SBOX_MAX;i++){
		BMP_MENULIST_Delete(wk->slist[i]);
		wk->slist[i] = NULL;
	}
}

/**
 *	@brief	セルアクターシステム初期化
 */
static void sub_ActorSysInit(NP_APPWORK* wk)
{
	initVramTransferManagerHeap(32,wk->heapID);

	//セルアクターシステム初期化
	wk->pActSys = CATS_AllocMemory(wk->heapID);

	CATS_SystemInit( wk->pActSys,
		&nplanter_coi,&nplanter_ccmm,NPLANTER_ACTPAL_NUM );

	//OAM初期化
	REND_OAM_UtilOamRamClear_Main(wk->heapID);
	REND_OAM_UtilOamRamClear_Sub(wk->heapID);

	//セルアクターリソース初期化
	wk->pActRes = CATS_ResourceCreate(wk->pActSys);
	CATS_ClactSetInit( wk->pActSys, wk->pActRes,NP_ACTOR_MAX);

	CATS_ResourceLoadBinaryEx( wk->pActSys, wk->pActRes,
		&nplanter_actres_list,CATS_CGXOFS_AREA,CATS_PLTTOFS_AREA);

	//アーカイブのハンドルを取得
	wk->icon_h = ArchiveDataHandleOpen(ARC_ITEMICON,wk->heapID);

	// アイテムアイコンリソース初期化
	{
		const TCATS_RESOURCE_NUM_LIST resNum = {
			1,	///< キャラの数
			1,	///< パレットの数
			1,	///< セルの数
			1,	///< セルアニメの数
			0,	///< マルチセルの数
			0,	///< マルチセルアニメの数
		};

		wk->pActIconRes = CATS_ResourceCreate(wk->pActSys);
		CATS_ResourceManagerInit(wk->pActSys,wk->pActIconRes,&resNum);
		//セルアクターセットは流用
		CATS_SetClactSetPtr(wk->pActIconRes,CATS_GetClactSetPtr(wk->pActRes));


		CATS_LoadResourceCharArcH(
			wk->pActSys, wk->pActIconRes, wk->icon_h,
			GetItemIndex(ITEM_KURABONOMI,ITEM_GET_ICON_CGX), 0, NNS_G2D_VRAM_TYPE_2DMAIN, ACTRES_ITEMICON );

		CATS_LoadResourcePlttArcH(
			wk->pActSys, wk->pActIconRes, wk->icon_h,
			GetItemIndex(ITEM_KURABONOMI,ITEM_GET_ICON_PAL), 0, 1, NNS_G2D_VRAM_TYPE_2DMAIN, ACTRES_ITEMICON );

		CATS_LoadResourceCellArcH(
			wk->pActSys, wk->pActIconRes, wk->icon_h, ItemIconCellGet(), 0, ACTRES_ITEMICON );
		CATS_LoadResourceCellAnmArcH(
			wk->pActSys, wk->pActIconRes, wk->icon_h, ItemIconCAnmGet(), 0, ACTRES_ITEMICON );
	}
	wk->sbox = SelectBoxSys_AllocWork( wk->heapID, NULL );
}

/**
 *	@brief	セルアクターシステム解放
 */
static void sub_ActorSysRelease(NP_APPWORK* wk)
{
	SelectBoxSys_Free(wk->sbox);

	//ハンドル解放
	ArchiveDataHandleClose( wk->icon_h );

	//リソース解放
	CATS_ResourceDestructor_S(wk->pActSys,wk->pActIconRes);
	CATS_ResourceDestructor(wk->pActSys,wk->pActRes);
	wk->pActIconRes = NULL;
	wk->pActRes = NULL;

	//システムワーク解放
	CATS_FreeMemory(wk->pActSys);
	wk->pActSys = NULL;
	DellVramTransferManager();

	//OAM初期化
	REND_OAM_UtilOamRamClear_Main(wk->heapID);
}
/*
 *	@brief	セルアクター初期化
 */
static void sub_ActorInit(NP_APPWORK* wk)
{
	sub_ActorSysInit(wk);
	sub_ActorObjMake(wk);

	GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );
}

/**
 *	@brief	セルアクター解放
 */
static void sub_ActorRelease(NP_APPWORK* wk)
{
	GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_OFF );

	sub_ActorObjDelete(wk);
	sub_ActorSysRelease(wk);
}

/**
 *	@brief	セルアクターオブジェクト作成
 */
static void sub_ActorObjMake(NP_APPWORK* wk)
{
	int i;
	u8	idx;

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
	{
		{	// 戻るボタン
			ACTRES_NORMAL,	//ID
			ACT_BACK_PX, ACT_BACK_PY, 0,	//x,y,z
			ANM_BACK, ACT_SPRI_BACK, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			ACTRES_NORMAL,	//ID
			ACT_CUR_PX, ACT_CUR_PY, 0,	//x,y,z
			ANM_CUR01, ACT_SPRI_CURSOR, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// じょうろ
			ACTRES_NORMAL,	//ID
			ACT_POT_PX, ACT_POT_PY, 0,	//x,y,z
			ANM_POT01, ACT_SPRI_POT, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 土
			ACTRES_NORMAL,	//ID
			ACT_SOIL_PX, ACT_SOIL_PY, 0,	//x,y,z
			ANM_SOIL, ACT_SPRI_SOIL, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// キラキラ
			ACTRES_NORMAL,	//ID
			ACT_KIRA_PX, ACT_KIRA_PY, 0,	//x,y,z
			ANM_KIRA, ACT_SPRI_KIRA, 0, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
	};

	for(i = 0;i <= ACT_CURSOR;i++){
		wk->pAct[i] = CATS_ObjectAdd( wk->pActSys, wk->pActRes, &ActAddParam[i] );
//		CLACT_SetAnmFrame(wk->pAct[i], FX32_ONE);
		CLACT_SetDrawFlag(wk->pAct[i], TRUE );
		CLACT_SetAnmFlag(wk->pAct[i], TRUE);
//		CLACT_BGPriorityChg( wk->pAct[ACT_SHADOW], DFRM_EFF_PRI );
	}
	wk->pAct[ACT_POT] = CATS_ObjectAdd( wk->pActSys, wk->pActRes, &ActAddParam[2] );
	CLACT_SetDrawFlag(wk->pAct[ACT_POT], FALSE );
	CLACT_SetAnmFlag(wk->pAct[ACT_POT], FALSE);

	//土とキラキラ
	for(i = 0;i < SEEDBED_MAX;i++){
		idx = ACT_SOIL01+i;
		wk->pAct[idx] = CATS_ObjectAdd( wk->pActSys, wk->pActRes, &ActAddParam[3]);

		CATS_ObjectPosSet(wk->pAct[idx],ACT_SOIL_PX+ACT_SOIL_OX*i,ACT_SOIL_PY);
		CLACT_AnmFrameSet( wk->pAct[idx] , wk->seed[i].ground_st);
		CLACT_SetDrawFlag(wk->pAct[idx], TRUE );
		CLACT_SetAnmFlag(wk->pAct[idx], FALSE);

		idx = ACT_KIRA01+i;
		wk->pAct[idx] = CATS_ObjectAdd( wk->pActSys, wk->pActRes, &ActAddParam[4]);

		CATS_ObjectPosSet(wk->pAct[idx],ACT_KIRA_PX+ACT_KIRA_OX*i,ACT_KIRA_PY);
		CLACT_SetDrawFlag(wk->pAct[idx], FALSE );
		CLACT_SetAnmFlag(wk->pAct[idx], TRUE);
	}
	//アイコンアクター
//	wk->pIconAct[0] = sub_IconActorAdd(wk,ACT_ICON_PX,ACT_ICON_PY,0);
#if 0
	//パネルセルのキャラクタリソース読み替え
	CATS_ChangeResourceCharArc(
		wk->pActSys, wk->pActRes, ARC_AN_PUZZLE_GRA,
		NARC_an_puzzle_gra_oam_p01_ncgr+wk->level,FALSE, ACTRES_PUZZLE01);

	//パズルパネル作成
	for(i = 0;i < PANEL_MAX;i++){
		u8 idx;

		idx = i+ACT_PANEL01;
		wk->pAct[idx] = CATS_ObjectAdd( wk->pActSys,wk->pActRes,&ActAddParam[2] );
		CLACT_SetDrawFlag(wk->pAct[idx], TRUE );
		CLACT_AnmChg(wk->pAct[idx], i );
		CLACT_ObjModeSet( wk->pAct[idx],GX_OAM_MODE_NORMAL);

		//回転させるとドット欠けするので倍角設定にする
		CLACT_SetAffineParam(  wk->pAct[idx], CLACT_AFFINE_DOUBLE);

		//パネルワークにアクターポインタ設定
		wk->panel[i].act = wk->pAct[idx];

		//初期表示位置セット
		sub_PanelGridPosSet(wk,i,wk->panel[i].x,wk->panel[i].y,wk->panel[i].rot);
	}
#endif
}

/**
 *	@brief	セルアクターオブジェクト削除
 */
static void sub_ActorObjDelete(NP_APPWORK* wk)
{
	int i;

//	CATS_ActorPointerDelete_S(wk->pIconAct[0]);

	for(i = 0;i < NPLANTER_ACTMAX;i++){
		if(wk->pAct[i] != NULL){
			CLACT_Delete(wk->pAct[i]);
		}
	}
}

/**
 *	@brief	木の実アクターリソース読み替え
 */
static void sub_SeedActorResChange(NP_APPWORK* wk, BED_ID id)
{
	u8 type = wk->seed[id].type;

	//木の実タイプは1オリジンなので-1
	if(type){
		--type;
	}
	CATS_ChangeResourceCharArcH( wk->pActSys, wk->pActRes, wk->seed_h,
		NARC_nplanter_gra_seed_01_ncgr+type, FALSE, ACTRES_SEED01+id );
	CATS_ChangeResourcePlttArcH( wk->pActSys, wk->pActRes, wk->seed_h,
		NARC_nplanter_gra_seed_01_nclr+type, FALSE, ACTRES_SEED01+id );
}

/**
 *	@brief	アイコンアクターリソース読み替え
 */
static void sub_IconActorResChange(NP_APPWORK* wk, BED_ID id)
{
	u16 item = SeedSys_Nuts2Item(wk->seed[id].type);

	CATS_ChangeResourceCharArcH( wk->pActSys, wk->pActIconRes, wk->icon_h,
		GetItemIndex(item,ITEM_GET_ICON_CGX), 0, ACTRES_ITEMICON);

	CATS_ChangeResourcePlttArcH( wk->pActSys, wk->pActIconRes, wk->icon_h,
		GetItemIndex(item,ITEM_GET_ICON_PAL), 0, ACTRES_ITEMICON);
}


/**
 *	@brief	アイコンアクターオブジェクト生成
 */
static CATS_ACT_PTR sub_IconActorAdd(NP_APPWORK* wk,s16 x,s16 y,u8 spri)
{
	TCATS_OBJECT_ADD_PARAM_S prm;

#if 0
	CATS_ChangeResourceCharArcH( wk->pActSys, wk->pActIconRes, wk->icon_h,
		GetItemIndex(item,ITEM_GET_ICON_CGX), 0, ACTRES_ITEMICON);

	CATS_ChangeResourcePlttArcH( wk->pActSys, wk->pActIconRes, wk->icon_h,
		GetItemIndex(item,ITEM_GET_ICON_PAL), 0, ACTRES_ITEMICON);
#endif
	MI_CpuClear8(&prm,sizeof(TCATS_OBJECT_ADD_PARAM_S));

	prm.x = x;	///< [ X ] 座標
	prm.y = y;	///< [ Y ] 座標
	prm.z = 0;	///< [ Z ] 座標

	prm.anm = 0;					///< アニメ番号
	prm.bg_pri = 2;					///< 優先順位
	prm.pal = 0;//CATS_PlttID_NoGet(wk->pActIconRes,ACTRES_ITEMICON, NNS_G2D_VRAM_TYPE_2DMAIN);		///< パレット番号
	prm.vram_trans = 0;	///< 描画エリア
	prm.d_area = NNS_G2D_VRAM_TYPE_2DMAIN;	///< 描画エリア

	prm.id[0] = ACTRES_ITEMICON;	//char
	prm.id[1] = ACTRES_ITEMICON;	//Pal
	prm.id[2] = ACTRES_ITEMICON;	//Cell
	prm.id[3] = ACTRES_ITEMICON;	//Cell Anime
	prm.id[4] = CLACT_U_HEADER_DATA_NONE;	//Multi Cell
	prm.id[5] = CLACT_U_HEADER_DATA_NONE;	//Muiti Cell Anime

	return CATS_ObjectAdd_S(wk->pActSys,wk->pActIconRes,&prm);
}

/**
 *	@brief	水遣りポット移動エフェクトタスク生成
 */
static void sub_PotMoveEffSet(NP_APPWORK* wk)
{
	POT_MOVE_EFF* ep;
	s8	dis;
	u8	spos,epos;

	ep = sys_AllocMemory(wk->heapID,sizeof(POT_MOVE_EFF));
	MI_CpuClear8(ep,sizeof(POT_MOVE_EFF));

	ep->app_wk = wk;
	ep->now_pos = wk->pot_pos;
	ep->next_pos = wk->pot_npos;

	dis = wk->pot_npos - wk->pot_pos;

	if(dis > 0){
		ep->dir = 1;
		spos = wk->pot_pos;
		epos = wk->pot_npos;
	}else{
		ep->dir = -1;
		dis = dis*-1;
		spos = wk->pot_npos;
		epos = wk->pot_pos;
	}
	dis *= ACT_POT_OX;
	ep->time = dis/ACT_POT_SPDX;
	ep->old_pos = wk->pot_pos;

	TCB_Add( tcb_PotMoveEff, ep, 0);
	++wk->eff_tcb_ct;
}

/**
 *	@brief	水遣りポット移動エフェクト
 */
static void tcb_PotMoveEff( TCB_PTR tcb, void* p_work )
{
	u8	n_pos;
	s16	px,py;
	POT_MOVE_EFF* ep = (POT_MOVE_EFF*)p_work;
	NP_APPWORK* wk = ep->app_wk;

	CATS_ObjectPosMove(wk->pAct[ACT_POT], ep->dir*ACT_POT_SPDX, 0);

	//現在位置取得
	CATS_ObjectPosGet(wk->pAct[ACT_POT], &px, &py);

	n_pos = (px-ACT_POT_PX)/ACT_POT_OX;
	if(n_pos != ep->old_pos && n_pos != ep->next_pos){
		//途中経路にも水遣りをする
		NPSub_Water( wk ,n_pos);
		ep->old_pos = n_pos;
	}

	if(--ep->time > 0){
		return;
	}
	CATS_ObjectPosSet(wk->pAct[ACT_POT],ep->next_pos*ACT_POT_OX+ACT_POT_PX,ACT_POT_PY);

	--wk->eff_tcb_ct;

	// メモリ解放
	sys_FreeMemoryEz(p_work);
	TCB_Delete( tcb );
}

/**
 *	@brief	木の実収穫エフェクトタスク生成
 */
static void sub_HarvestIconEffSet(NP_APPWORK* wk)
{
	HARVEST_ICON_EFF* ep;

	ep = sys_AllocMemory(wk->heapID,sizeof(HARVEST_ICON_EFF));
	MI_CpuClear8(ep,sizeof(HARVEST_ICON_EFF));

	ep->app_wk = wk;
	ep->id = wk->c_pos;

	TCB_Add( tcb_HarvestIconEff, ep, 0);
	++wk->eff_tcb_ct;
}

/**
 *	@brief	木の実状態更新エフェクト
 */
static void tcb_HarvestIconEff( TCB_PTR tcb, void* p_work )
{
	HARVEST_ICON_EFF* ep = (HARVEST_ICON_EFF*)p_work;
	NP_APPWORK* wk = ep->app_wk;

	switch(ep->seq){
	case 0:
		ep->act = sub_IconActorAdd( wk,
			ACT_ICON_PX + (ACT_ICON_OX*ep->id),ACT_ICON_PY, ACT_SPRI_EFF + wk->harvest_num);
		Snd_SePlay(SND_NP_HARVEST);
		ep->seq++;
		break;
	case 1:
		CATS_ObjectPosMoveCap(ep->act, 2, -2);
		if(ep->ct++ < 8){
			break;
		}
		ep->seq++;
		break;
	case 2:
		CATS_ActorPointerDelete_S(ep->act);
		--wk->eff_tcb_ct;

		// メモリ解放
		sys_FreeMemoryEz(p_work);
		TCB_Delete( tcb );
	}
}

/**
 *	@brief	メッセージデータ生成
 */
static void sub_MsgMake(NP_APPWORK* wk,MSG_IDX idx,BED_ID bed_id)
{
	SEED_STATE* st = &wk->seed[bed_id];

	//常駐展開

	switch(idx){
	case MIDX_BS_COMPOST:
	case MIDX_RA_COMPOST:
		WORDSET_RegisterItemName( wk->msg.wset, 2, SeedSys_Compost2Item(st->compost) );
		break;
	case MIDX_BS_GROWS05:
	case MIDX_RA_HARVEST01:
		WORDSET_RegisterNumber( wk->msg.wset, 1,
				st->nuts_ct , 2, NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_ZENKAKU );
		//↓スルー
	case MIDX_BS_GROWS01:
	case MIDX_BS_GROWS02:	//双葉
	case MIDX_BS_GROWS03:
	case MIDX_BS_GROWS04:
	case MIDX_RA_PLANT:
	case MIDX_RA_HARVEST02:
	case MIDX_RA_HARVEST03:
		{
		STRBUF * buf = NutsNameGetAlloc( st->type, wk->heapID);
		WORDSET_RegisterWord(wk->msg.wset, 0, buf, PM_MALE, FALSE, PM_LANG);
		STRBUF_Delete(buf);
		}
		break;
	default:
		STRBUF_Copy( wk->msg.tmp, wk->msg.exp[idx] );
		return;
	}
	WORDSET_ExpandStr( wk->msg.wset, wk->msg.tmp ,wk->msg.exp[idx]);
}


/********************************************************************************/
//////////////////////////////////////////////////////////////////////////////////
//デバッグ用
//////////////////////////////////////////////////////////////////////////////////
/********************************************************************************/
#ifdef PM_DEBUG
static void deb_BmpWinAdd(NP_APPWORK* wk)
{
	int i;
	static const BMPWIN_DAT BmpWinData[] = {
	 {	// 「デバッグ情報ウィンドウ」
		BMP_DEBT_FRM, BMP_DEBT_PX, BMP_DEBT_PY,
		BMP_DEBT_SX, BMP_DEBT_SY, BMP_DEBT_PAL, BMP_DEBT_CGX
	 },
	 {	// 「きのみ情報ウィンドウ」
		BMP_DEBS_FRM, BMP_DEBS_PX, BMP_DEBS_PY01,
		BMP_DEBS_SX, BMP_DEBS_SY, BMP_DEBS_PAL, BMP_DEBS_CGX01
	 },
	 {	// 「きのみ情報ウィンドウ」
		BMP_DEBS_FRM, BMP_DEBS_PX, BMP_DEBS_PY02,
		BMP_DEBS_SX, BMP_DEBS_SY, BMP_DEBS_PAL, BMP_DEBS_CGX02
	 },
	 {	// 「きのみ情報ウィンドウ」
		BMP_DEBS_FRM, BMP_DEBS_PX, BMP_DEBS_PY03,
		BMP_DEBS_SX, BMP_DEBS_SY, BMP_DEBS_PAL, BMP_DEBS_CGX03
	 },
	 {	// 「きのみ情報ウィンドウ」
		BMP_DEBS_FRM, BMP_DEBS_PX, BMP_DEBS_PY04,
		BMP_DEBS_SX, BMP_DEBS_SY, BMP_DEBS_PAL, BMP_DEBS_CGX04
	 },
	};

	for( i=0; i < DEB_WIN_MAX; i++ ){
		GF_BGL_BmpWinAddEx( wk->bgl, &wk->deb_win[i], &BmpWinData[i] );
		GF_BGL_BmpWinDataFill( &wk->deb_win[i], 0 );
	}
}

/**
 *	@brief	デバッグプリントウィンドウ破棄
 */
static void deb_BmpWinDel(NP_APPWORK* wk)
{
	int i;
	for( i=0; i < DEB_WIN_MAX; i++ ){
		GF_BGL_BmpWinOff( &wk->deb_win[i] );
		GF_BGL_BmpWinDel( &wk->deb_win[i] );
	}
}


#endif	//PM_DEBUG

